const {
	exec,
	escape
} = require('../mysql.js')
const {
	formatTime
} = require('../../utils/time.js')
// 创建博客
function createBlog(userId,title, content, author, tag='') {
	let time = new Date()
	let createTime = formatTime(time)
	createTime = escape(createTime)
	title = escape(title)
	userId = escape(userId)	
	content = escape(content)
	author = escape(author)
	tag = escape(tag)
	console.log('createTime',createTime);
	let sql =
		`insert into blogs (user_id,title,content,author,create_time,tag) values (${userId},${title},${content},${author},${createTime},${tag});`
		console.log('sql',sql);
		return exec(sql)
}


// 查询博客
function queryBlog(userId,tags){
	userId = escape(userId)
	
	let sql = `SELECT * FROM blogs WHERE user_id = ${userId}`
	tags.forEach(item=>{
		sql+=` AND tag LIKE '%${item}%'`
	})
	console.log('博客查询sql',sql);
	return exec(sql)
}

module.exports={
	createBlog,
	queryBlog
}